function showPostModal() {
    modalBody.innerHTML = `
        <h2>上传题目图片</h2>
        <div class="form-group">
            <input type="file" id="questionImage" accept="image/*"><br><br>
            <input type="text" id="questionText" placeholder="输入你的问题"><br><br>
            <label for="questionType">题目类型：</label>
            <select id="questionType">
                <option value="voice">语音</option>
                <option value="text">文本</option>
            </select><br><br>
        </div>
        <button id="qaConfirm">确认</button>
    `;

    modal.style.display = 'block';

    document.getElementById('qaConfirm').addEventListener('click', sendPostRequest);
}

async function sendPostRequest() {
    const imageFile = document.getElementById('questionImage').files[0];
    const questionText = document.getElementById('questionText').value;
    const questionType = document.getElementById('questionType').value;

    modal.style.display = 'none';

    const ansDiv = document.createElement('div');
    ansDiv.className = 'question';
    ansDiv.innerText = '正在处理，请稍后...\n'; // 初始化为空
    // modalBody.innerText = '';
    document.getElementById('content').appendChild(ansDiv);

    const formData = new FormData();
    if (imageFile) {
        formData.append('image', imageFile);
    }
    formData.append('text', questionText);
    formData.append('type', questionType);

    try {
        const response = await fetch('/api/qa', {
            method: 'POST',
            headers: {
                
            },
            body: formData,
        });

        if (!response.ok) {
            modalBody.innerText = `错误: ${response.statusText}`;
            return;
        }
        let fullText = "";
        let taskId = null;

        const reader = response.body.getReader();
        const decoder = new TextDecoder("utf-8");
        while (true) {
            const { value, done } = await reader.read();
            if (done) break;

            const chunk = decoder.decode(value, { stream: true });
            if (chunk.startsWith("[task_id]:")) {
                taskId = chunk.split(":")[1].trim();
                continue;
            }

            // modalBody.innerText += chunk;
            ansDiv.innerText += chunk;
            fullText += chunk;
        }

        // 流式结束后，请求音频
        if (questionType !== "text" && taskId) {
            // alert("正在获取音频，请稍候...");
            // modalBody.innerText = '';
            await new Promise(r => setTimeout(r, 500));
            const audioResp = await fetch("/api/qa/audio", {
                method: "POST",
                headers: {
                    "Authorization": `Bearer ${localStorage.getItem('token')}`,
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({ task_id: taskId })
            });

            if (audioResp.ok) {
                const audioBlob = await audioResp.blob();
                const audioUrl = URL.createObjectURL(audioBlob);

                const audioElem = document.createElement("audio");
                audioElem.src = audioUrl;
                audioElem.controls = true;

                // modalBody.appendChild(document.createElement("hr"));
                document.getElementById('content').appendChild(audioElem);
                document.getElementById('content').appendChild(document.createElement("hr"));
                // 强制加载
                audioElem.load(); 
            } else {
                document.getElementById('content').innerText += "\n[音频获取失败]:"+audioResp.json().error;
            }
        }
    } catch (error) {
        modalBody.innerText = `请求失败: ${error.message}`;
    }
}
